/**
 * Author: zhezhen.cheng
 * Creation Time: 2016/6/14.
 * Description:...
 */
define(['jquery','../m/wirelessM'],
    function ($,wirelessM) {
        function wirelessV(datas) {
            var checkVal = (datas.data.wifi_enable != null ) ? datas.data.wifi_enable : 0;
            var ischeck = (datas.data.wifi_enable != null && datas.data.wifi_enable !=0) ? 'check-span-current' : '';
            var wifi_ssids = (datas.data.wifi_ssid != null) ? datas.data.wifi_ssid : '';
            var wifi_modes = (datas.data.wifi_mode != null) ? datas.data.wifi_mode : '';
            var wifi_modes_text = '';
            if(datas.data.wifi_mode == 'bgn'){
                wifi_modes_text = '11b/g/n 混合模式（推荐）';
            }else if(datas.data.wifi_mode == 'bg'){
                wifi_modes_text = '11b/g 混合模式';
            }else if(datas.data.wifi_mode == 'b'){
                wifi_modes_text = '11b 模式';
            }else if(datas.data.wifi_mode == 'g'){
                wifi_modes_text = '11g 模式';
            }
            var wifi_channels = (datas.data.wifi_channel != null) ? datas.data.wifi_channel : '';
            var wifi_channels_text = '';
            switch (datas.data.wifi_channel)
            {
                case '0':
                    wifi_channels_text = '自动';
                    break;
                case '1':
                    wifi_channels_text = '1 CH';
                    break;
                case '2':
                    wifi_channels_text = '2 CH';
                    break;
                case '3':
                    wifi_channels_text = '3 CH';
                    break;
                case '4':
                    wifi_channels_text = '4 CH';
                    break;
                case '5':
                    wifi_channels_text = '5 CH';
                    break;
                case '6':
                    wifi_channels_text = '6 CH';
                    break;
                case '7':
                    wifi_channels_text = '7 CH';
                    break;
                case '8':
                    wifi_channels_text = '8 CH';
                    break;
                case '9':
                    wifi_channels_text = '9 CH';
                    break;
                case '10':
                    wifi_channels_text = '10 CH';
                    break;
                case '11':
                    wifi_channels_text = '11 CH';
                    break;
                case '12':
                    wifi_channels_text = '12 CH';
                    break;
                case '13':
                    wifi_channels_text = '13 CH';
                    break;
            }
            var wifi_auths = (datas.data.wifi_auth != null) ? datas.data.wifi_auth : '';
            var wifi_auths_text = '';
            if(datas.data.wifi_auth == 0){
                wifi_auths_text ='20 MHz';
            }else{
                wifi_auths_text ='20/40 MHz';
            }
            var wifi_entrys = (datas.data.wifi_entry != null) ? datas.data.wifi_entry : '';
            var wifi_entrys_text = '';
            if(datas.data.wifi_entry == 'NONE'){
                wifi_entrys_text = '无加密';
            }else if(datas.data.wifi_entry == 'WPAPSKWPA2PSK'){
                wifi_entrys_text = 'WPA1/WPA2 强加密';
            }else if(datas.data.wifi_entry == 'WPAPSK'){
                wifi_entrys_text = 'WPA1加密';
            }else if(datas.data.wifi_entry == 'WPA2PSK'){
                wifi_entrys_text = 'WPA2加密';
            }
            var wifi_passer = (datas.data.wifi_pass != null) ? datas.data.wifi_pass : '';
            var wifi_beacons = (datas.data.wifi_beacon != null) ? datas.data.wifi_beacon : '';
            var html = '';
            html +='<div class="header">';
            html +='<a class="back-a" href="/h5-wizard/index.html#advanced-setting"></a><p>连接网络</p><a class="header-right">保存</a>';
            html +='</div>';
            html +='<div class="content">';
            html +='<div class="success-txt m-t-6"><i>WiFi开关</i><span class="check-span '+ischeck+'"><input type="hidden" value="'+checkVal+'" class="wifi_enable"></span></div>';
            html +='<div class="ly-pppoe content-ly-pppoe">';
            html +='<p>基本设置</p>';
            html +='<ul>';
            html +='<li><i>WiFi名称</i><div class="select"><input type="text" class="wifi_ssid" placeholder="WiFi名称" value="'+wifi_ssids+'"></div></li>';
            html +='<li><i>模式</i><div class="select"><input type="text" class="wifi_mode" readonly="readonly" placeholder="请选择模式" value="'+wifi_modes_text+'" vals="'+wifi_modes+'"><span id="chose-wifi" class=""></span>';
            html +='<ul class="name-list" style="display: none;">';
            html +='<li value="bgn">11b/g/n 混合模式（推荐）</li>';
            html +='<li value="bg">11b/g 混合模式</li>';
            html +='<li value="b">11b 模式</li>';
            html +='<li value="g">11g 模式</li>';
            html +='</ul>';
            html +='</div></li>';
            html +='<li><i>信道</i><div class="select"><input type="text" class="wifi_channel" readonly="readonly" placeholder="请选择信道" value="'+wifi_channels_text+'" vals="'+wifi_channels+'"><span id="chose-wifi" class=""></span>';
            html +='<ul class="name-list" style="display: none;">';
            html +='<li value="0">自动</li>';
            html +='<li value="1">1 CH</li>';
            html +='<li value="2">2 CH</li>';
            html +='<li value="3">3 CH</li>';
            html +='<li value="4">4 CH</li>';
            html +='<li value="5">5 CH</li>';
            html +='<li value="6">6 CH</li>';
            html +='<li value="7">7 CH</li>';
            html +='<li value="8">8 CH</li>';
            html +='<li value="9">9 CH</li>';
            html +='<li value="10">10 CH</li>';
            html +='<li value="11">11 CH</li>';
            html +='<li value="12">12 CH</li>';
            html +='<li value="13">13 CH</li>';
            html +='</ul></div>';
            html +='</li>';
            html +='<li><i>频段</i><div class="select"><input type="text" class="wifi_auth" readonly="readonly" placeholder="请选择频段" value="'+wifi_auths_text+'" vals="'+wifi_auths+'"><span id="chose-wifi" class=""></span>';
            html +='<ul class="name-list" style="display: none;">';
            html +='<li value=0>20 MHz</li>';
            html +='<li value=1>20/40 MHz</li>';
            html +='</ul>';
            html +='</div>';
            html +='</li>';
            html +='</ul>';
            html +='</div>';
            html +='<div class="ly-pppoe content-ly-pppoe padding-wrieless">';
            html +='<p>安全设置</p>';
            html +='<ul>';
            html +='<li><i>加密方式</i><div class="select"><input type="text" class="wifi_entry" readonly="readonly" placeholder="请选择加密方式" value="'+wifi_entrys_text+'" vals="'+wifi_entrys+'"><span id="chose-wifi" class=""></span>';
            html +='<ul class="name-list" style="display: none;">';
            html +='<li value="NONE">无加密</li>';
            html +='<li value="WPAPSKWPA2PSK">WPA1/WPA2 强加密</li>';
            html +='<li value="WPAPSK">WPA1加密</li>';
            html +='<li value="WPA2PSK">WPA2加密</li>';
            html +='</ul></div>';
            html +='</li>';
            html +='<li><i>WiFi密码</i><div class="select"><input type="text" class="wifi_pass" placeholder="密码至少8位字符，英文、数字" value="'+wifi_passer+'"></div></li>';
            html +='<li><i>密码更新周期</i><div class="select"><input type="text" class="wifi_beacon" placeholder="单位秒，最小30,0为不更新" value="'+wifi_beacons+'"></div>';
            html +='</li>';
            html +='</ul>';
            html +='</div>';
            html +='</div>';
            html +='<div class="footer">';
            html +='<div>';
            html +='<span class="footer-icon footer-icon-one"></span>';
            html +='<p>系统状态</p>';
            html +='</div>';
            html +='<div>';
            html +='<span class="footer-icon footer-icon-two"></span>';
            html +='<p>线装管理</p>';
            html +='</div>';
            html +='<div>';
            html +='<span class="footer-icon footer-icon-three"></span>';
            html +='<p>数据上报</p>';
            html +='</div>';
            html +='<div class="footer-Er">';
            html +='<span class="footer-icon footer-icon-four"></span>';
            html +='<p>高级设置</p>';
            html +='</div>';
            html +='</div>';
            $(".webApp-content").html(html);
            $(".m-t-6").on('click','.check-span',function(){
                var input_obj = $(this).find('input');
                if($(this).hasClass('check-span-current')){
                    $(this).removeClass('check-span-current');
                    input_obj.val('0');
                }else{
                    $(this).addClass('check-span-current');
                    input_obj.val('1');
                }
            });
            $(".webApp-content").on('click','.select',function(event){
                var select_obj = $(this).find('.name-list');
                $(".select").find('.name-list').hide();
                if(select_obj.css('display') == 'none'){
                    select_obj.show();
                }
                event.stopPropagation();
            });
            $(".webApp-content").on('click','.name-list li',function(event){
                var vals = $(this).attr('value');
                var text_val = $(this).text();
                $(this).closest('.select').find('input').val(text_val);
                $(this).closest('.select').find('input').attr('vals',vals);
                $(this).parent().hide();
            });
            $(document).click(function(event){
                $(".select").find('.name-list').hide();
            });
            $(".header-right").on('click',function(){
                var wifi_ssid = $(".wifi_ssid").val();
                var wifi_channel = parseInt($(".wifi_channel").attr('vals'));
                var wifi_auth = parseInt($(".wifi_auth").attr('vals'));
                var wifi_entry = $(".wifi_entry").attr('vals');
                var wifi_pass = $(".wifi_pass").val();
                var wifi_beacon = parseInt($(".wifi_beacon").val());
                var wifi_enable = parseInt($(".wifi_enable").val());
                var wifi_mode = $(".wifi_mode").attr('vals');
                var data = JSON.stringify({
                    "wifi_enable": wifi_enable,
                    "wifi_ssid": wifi_ssid,
                    "wifi_mode": wifi_mode,
                    "wifi_pass": wifi_pass,
                    "wifi_channel": wifi_channel,
                    "wifi_auth": wifi_auth,
                    "wifi_entry": wifi_entry,
                    "wifi_beacon": wifi_beacon
                })
                wirelessM(data);
            });
        }

        return wirelessV;
    });